package com.nondo.dean.product.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.nondo.dean.common.tool.AliyunOssTool;
import com.nondo.dean.product.domain.Product;
import com.nondo.dean.product.domain.ProductExample;
import com.nondo.dean.product.mapper.ProductExtendsMapper;
import com.nondo.dean.product.mapper.ProductMapper;
import com.nondo.dean.product.model.SlideBean;
import com.nondo.dean.product.model.param.PurchaseParam;
import com.nondo.dean.product.model.result.ProductResult;

/**
 * 采购管理业务类
 * 
 * @author zlj
 * @date 2018年4月10日
 */
@Component
public class PurchaseService {

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    AliyunOssTool aliyunOssTool;

    @Autowired
    private ProductMapper productMapper;
    
    @Autowired
    private ProductExtendsMapper productExtendsMapper;
    
    @Autowired
    private PlatformService platformService;
    
    private final static int OFFSET = 0;
    
    /**
     * 畅销列表3条
     */
    private final static int BEST_SELLING_LIMIT = 3;
    
    /**
     * 最新列表4条
     */
    private final static int LASTEST_LIMIT = 4;
    
    /**
     * 推荐列表5条
     */
    private final static int RECOMMEND_LIMIT = 6;
    
    /**
     * 数据统计
     *
     * @param {@link PurchaseParam}
     * @return
     */
    public Long count(PurchaseParam purchaseParam) {
        ProductExample example = new ProductExample();
        ProductExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, purchaseParam);
        return productMapper.countByExample(example);
    }

    /**
     * 获取列表
     *
     * @param offset
     * @param limit
     * @param {@link PurchaseParam}
     * @return
     */
    public List<ProductResult> getList(int offset, int limit, PurchaseParam purchaseParam) {
        String clause = "ID DESC";
        Integer type = purchaseParam.getType();
        if (type != null) {
            switch (type) {
                case 3: // 系列
                    purchaseParam.setRecommend(1);
                    clause = "ID DESC";
                    break;
                case 1: // 畅销
                    clause = "SALE_NUM DESC";
                    break;
                case 2: // 最新
                    clause = "CREATE_DATE DESC";
                    break;
                default:
                    break;
            }
        }
        
        ProductExample example = new ProductExample();
        example.setOrderByClause(clause);
        RowBounds rowBounds = new RowBounds(offset, limit);
        ProductExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, purchaseParam);
        List<Product> productList = productMapper.selectByExampleWithRowbounds(example, rowBounds);
        
        List<ProductResult> productResultList = covertToResultList(productList);
        return productResultList;
    }

    /**
     * 结果集类型转换
     * @param productList List<{@link Product}>
     * @return 转换后的List<{@link ProductResult}>
     */
    private List<ProductResult> covertToResultList(List<Product> productList) {
        List<ProductResult> productResultList = new ArrayList<ProductResult>();
        for (Product productDb : productList) {
            ProductResult productResult = ProductResult.covertToBean(productDb);
            productResult.setMainImage(aliyunOssTool.getFileUrl(productResult.getMainImage()));
            productResultList.add(productResult);
        }
        return productResultList;
    }

    /**
     * 设置查询条件
     *
     * @param criteria
     * @param productWithBLOBs
     */
    void setCriteria(ProductExample.Criteria criteria, PurchaseParam purchaseParam) {
        if (purchaseParam != null) {
            if (purchaseParam.getRecommend() != null) {
                criteria.andRecommendEqualTo(purchaseParam.getRecommend());
            }
        }
        criteria.andShelfStatusEqualTo(1);
        criteria.andDelFlagEqualTo(Product.DEL_FLAG_NORMAL);
    }

    /**
     * 获取采购产品列表
     * @return 包含畅销、最新、推荐产品列表的{@link Map}
     */
    public Map<String, Object> getProductList() {
        Map<String,Object> resultMap = new HashMap<String, Object>();
        PurchaseParam purchaseParam = new PurchaseParam();
        // 幻灯片列表
        List<SlideBean> slideList = platformService.getPurchaseSlideList();
        resultMap.put("slideList", slideList);
        
        // 畅销产品列表
        purchaseParam.setType(1);
        List<ProductResult> bestSellingList = getList(OFFSET, BEST_SELLING_LIMIT, purchaseParam);
        resultMap.put("bestSellingList", bestSellingList);
        // 最新产品列表
        purchaseParam.setType(2);
        List<ProductResult> latestList = getList(OFFSET, LASTEST_LIMIT, purchaseParam);
        resultMap.put("latestList", latestList);
        // 推荐产品列表
        purchaseParam.setType(3);
        List<ProductResult> recommendList = getList(OFFSET, RECOMMEND_LIMIT, purchaseParam);
        resultMap.put("recommendList", recommendList);
        return resultMap;
    }
    
    /**
     * 获取推荐产品列表
     * @return 推荐产品列表的List<{@link ProductResult}>
     */
    public List<Product> getRecommandProductList() {
        ProductExample example = new ProductExample();
        ProductExample.Criteria criteria = example.createCriteria();
        PurchaseParam purchaseParam = new PurchaseParam();
        purchaseParam.setRecommend(1);
        setCriteria(criteria, purchaseParam);
        List<Product> productList = productMapper.selectByExample(example);
        return productList;
    }
    
    /**
     * 按照商品名/系列/商品编码搜索产品列表条数统计
     *
     * @param {@link PurchaseParam}
     * @return
     */
    public Long searchListCount(PurchaseParam purchaseParam) {
        return productExtendsMapper.countByExample(purchaseParam);
    }
    
    /**
     * 获取产品分页列表
     *
     * @param offset
     * @param limit
     * @param {@link PurchaseParam}
     * @return
     */
    public List<ProductResult> getSearchList(int offset, int limit, PurchaseParam purchaseParam) {
        RowBounds rowBounds = new RowBounds(offset, limit);
        List<Product> productList = productExtendsMapper.selectByExampleWithRowbounds(purchaseParam, rowBounds);
        
        List<ProductResult> productResultList = covertToResultList(productList);
        return productResultList;
    }

}
